import { Component, OnInit } from '@angular/core';
import {FileInfo, FileType} from "@shared/models/FileInfo";
import {DiskService} from "@core/service/DiskService";
import {BBYUtils} from "@shared/utils/BBYUtils";
import {NzContextMenuService, NzDropdownMenuComponent} from "ng-zorro-antd/dropdown";
import {SearchFileData, SharePageService, UserListData} from "@core/service/page/SharePageService";
import {Router} from "@angular/router";
import {NzMessageService} from "ng-zorro-antd/message";

@Component({
  selector: 'bby-search-file-list',
  templateUrl: './search-file-list.component.html',
  styleUrls: ['./search-file-list.component.scss']
})
export class SearchFileListComponent implements OnInit {
  pageData=new SearchFileData()
  checkedFile:FileInfo[]=[]
  checkedFilePath='';
  idPathMap=new Map<string,string>()
  constructor(
    private nzContextMenuService: NzContextMenuService,
    public pageService: SharePageService,
    public router: Router,
    private message: NzMessageService,
    private diskService:DiskService
  ) {
    this.pageData=pageService.searchFileData
  }
  contextMenu($event: MouseEvent, contextTemplate: NzDropdownMenuComponent, file: FileInfo) {
    this.nzContextMenuService.create($event, contextTemplate);
    this.fileClick(file)
  }
  ngOnInit(): void {
  }
  getImgURL(file: FileInfo) {
    return BBYUtils.getTypeImg(file);
  }
  setAllChecked(checked: boolean): void {
    this.pageData.list.forEach((item) => item.checked = checked);
  }

  fileClick(file: FileInfo) {
    this.setAllChecked(false)
    file.checked=true
    this.checkedFile=[file]
    let path = this.idPathMap.get(file.id)
    if (path){
      this.checkedFilePath=path
      return;
    }
    BBYUtils.handelResult(
      this.diskService.filePath(file.id),
      res => {
        this.checkedFilePath=res.data
        this.idPathMap.set(file.id,res.data)
      },
      res => {
        this.message.error("文件路径加载错误！")
      }
    )
  }
  openFile(){
    if (this.checkedFile.length==0){
      return;
    }
    let file = this.checkedFile[0]
    let fileId = file.isFile? file.parent:file.id
    let path = this.idPathMap.get(fileId)
    if (path){
      this.router.navigate([`${this.pageService.rootUrl}/${path}`])
      return;
    }
    BBYUtils.handelResult(
      this.diskService.filePath(fileId),
      res => {
        this.router.navigate([`${this.pageService.rootUrl}/${res.data}`],file.isFile? {queryParams:{check:file.id}}:undefined)
      },
      res => {
        this.message.error("文件路径加载错误！")
      }
    )
  }
  pageIndexChange() {
    let param = this.pageService.openParam
    console.log('search')
    this.router.navigate([this.pageService.openUrl],{
      queryParams:{
        page:this.pageData.listPage,
        search:param?.search||''
      }
    })
  }
}
